﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
namespace qiniuUI
{
    public partial class qiniuUI : Form
    {
        DispatcherTimer dispatcherTimer;
        public int count;//用于计时间
        public int left_click;
        public int mouse_mul_select;
        private string mybucket;
        private int right_click_index;
        qiniuOperation opera;
        download downloadUI;
        public qiniuUI()
        {
        }
        public qiniuUI(string ak, string sk, string bucket)
        {
            InitializeComponent();
            downloadUI = new download();
            this.FormBorderStyle = FormBorderStyle.Fixed3D;
            mybucket = bucket;
            opera = new qiniuOperation(ak, sk);
            List<string> lst = new List<string>();
            try
            {
                opera.list(mybucket, lst);
            }
            catch (Exception ex)
            {
                opera.except_process(ex);
                this.Close();
            }
            foreach (string i in lst)
            {
                filelist.Items.Add(i.ToString());
            }
        }

        private void qiniuUI_Load(object sender, EventArgs e)
        {
        }

        private void filelist_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        private void qiniuUI_FormClosed(object sender, FormClosingEventArgs e)
        {
        }

        private void update_filelist_Click(object sender, EventArgs e)
        {
            List<string> lst = new List<string>();
            output_name.Text = "操作状态";
            try
            {
                opera.list(mybucket, lst);
            }
            catch (Exception ex)
            {
                opera.except_process(ex);
                this.Close();
            }
            filelist.Items.Clear();
            output_information.Text = "检测到下面的文件" + Environment.NewLine;
            foreach (string i in lst)
            {
                filelist.Items.Add(i.ToString());
                output_information.Text = output_information.Text + i + Environment.NewLine;
            }
        }

        private void attribute_Click(object sender, EventArgs e)
        {
            List<string> attr_file_lst = new List<string>();
            List<string> attr_lst = new List<string>();
            string selected_txt = "";
            string sum = null;
            float dsum = 0;
            output_name.Text = "属性";
            int i = 0;
            for (i = 0; i < filelist.Items.Count; i++)
                if (filelist.GetItemChecked(i))
                {
                    opera.set_selected_items(attr_file_lst, filelist.GetItemText(filelist.Items[i]));
                }
            output_information.Text = "等待服务器反应中";
            try
            {
                if (attr_file_lst.Count == 1)
                {
                    opera.stat(mybucket, attr_file_lst[0], attr_lst);
                    sum = attr_lst[2];
                }
                if (attr_file_lst.Count > 1)
                {
                    sum = opera.batch_stat(mybucket, attr_file_lst);
                }
            }
            catch (Exception ex)
            {
                opera.except_process(ex);
                this.Close();
            }
            output_information.Text = "你选择中的文件是:";
            for (i = 0; i < attr_file_lst.Count; i++)
            {
                selected_txt += attr_file_lst[i] + "    ";
            }
            if (sum != null)
            {
                dsum = (float)Int64.Parse(sum) / 1024;
            }
            output_information.Text += "    " + selected_txt + Environment.NewLine;
            if (attr_file_lst.Count > 1)
            {
                output_information.Text += "总大小是:    ";
                output_information.Text += dsum + "KB";
            }
            else if (attr_file_lst.Count == 1)
            {
                //linux时间戳转换
                DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                long lTime = long.Parse(attr_lst[1]);
                TimeSpan toNow = new TimeSpan(lTime);
                dtStart = dtStart.Add(toNow);

                output_information.Text += "哈希数为：    " + attr_lst[0] + Environment.NewLine;
                output_information.Text += "上传时间为：    " + dtStart.ToLongDateString() + Environment.NewLine;
                output_information.Text += "文件大小为：    " + dsum + "KB" + Environment.NewLine;
                output_information.Text += "文件类型为：    " + attr_lst[3] + Environment.NewLine;
            }
            else
            {
                output_information.Text = "请选择文件~";
            }
        }
        private void notselected_Click(object sender, EventArgs e)
        {
            int i;
            for (i = 0; i < filelist.Items.Count; i++)
                if (filelist.GetItemChecked(i))
                {
                    filelist.SetItemChecked(i, false);
                }
                else
                {
                    filelist.SetItemChecked(i, true);
                }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            List<string> attr_file_lst = new List<string>();
            string selected_txt = null;
            output_name.Text = "操作状态";
            int i = 0;
            for (i = 0; i < filelist.Items.Count; i++)
                if (filelist.GetItemChecked(i))
                {
                    opera.set_selected_items(attr_file_lst, filelist.GetItemText(filelist.Items[i]));
                }
            output_information.Text = "等待服务器反应中";
            try
            {
                if (attr_file_lst.Count > 0)
                {
                    if (MessageBox.Show("你确定要删除选中的文件吗？", "提示：", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (attr_file_lst.Count == 1)
                        {
                            opera.delete(mybucket, attr_file_lst[0]);
                        }
                        if (attr_file_lst.Count > 1)
                        {
                            opera.batch_del(mybucket, attr_file_lst);
                        }
                    }
                    else
                    {
                        attr_file_lst.Clear();
                        selected_txt = "abort";
                    }
                }
            }
            catch (Exception ex)
            {
                opera.except_process(ex);
                this.Close();
            }
            for (i = 0; i < attr_file_lst.Count; i++)
            {
                filelist.Items.Remove(attr_file_lst[i]);
            }
            for (i = 0; i < attr_file_lst.Count; i++)
            {
                selected_txt += attr_file_lst[i] + "    ";
            }
            if (selected_txt == null)
            {
                selected_txt = "请选择删除文件~";
            }
            else
            {
                if (selected_txt != "abort")
                {
                    selected_txt = "你删除了：      " + selected_txt;
                }
                else
                {
                    selected_txt = "你取消删除操作";
                }
            }
            output_information.Text = selected_txt;
        }

        private void move_Click(object sender, EventArgs e)
        {
            List<string> move_file_lst = new List<string>();
            List<string> change_name = new List<string>();
            List<int> change_fail_index = new List<int>();
            string selected_txt = null;
            output_name.Text = "操作状态";
            int i = 0;
            for (i = 0; i < filelist.Items.Count; i++)
                if (filelist.GetItemChecked(i))
                {
                    opera.set_selected_items(move_file_lst, filelist.GetItemText(filelist.Items[i]));
                }
            output_information.Text = "等待服务器反应中";
            if (move_file_lst.Count > 0)
            {
                move_win mov_win = new move_win();
                this.Enabled = false;
                if (mov_win.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        opera.batch_mov(mybucket, move_file_lst, change_name, move_win.name_prefix, move_win.source_name_rule, move_win.name_num_rule, move_win.name_postfix);
                    }
                    catch (Exception ex)
                    {
                        opera.except_process(ex);
                        this.Close();
                    }
                    i = 0;
                    foreach (string name in change_name)
                    {
                        if (filelist.Items.Contains(name))
                        {
                            selected_txt += move_file_lst[i];
                            change_fail_index.Add(i);
                        }
                        else
                        {
                            filelist.Items.Add(name);
                        }

                        i++;
                    }
                    int j;
                    if (change_fail_index.Count > 0)
                    {
                        for (i = 0, j = 0; i < move_file_lst.Count; i++)
                        {
                            if (i == change_fail_index[j])
                            {
                                j++;
                            }
                            else
                            {
                                filelist.Items.Remove(move_file_lst[i]);
                            }
                        }
                    }
                    else
                    {
                        for (i = 0; i < move_file_lst.Count; i++)
                        {
                            filelist.Items.Remove(move_file_lst[i]);
                        }
                    }
                    if (selected_txt == null)
                    {
                        output_information.Text = "成功移动文件";
                    }
                    else
                    {
                        output_information.Text = "这些文件没有成功移动：      ";
                        output_information.Text += selected_txt;
                    }
                }
                else
                {
                    output_information.Text = "你取消了移动操作";
                }
                this.Enabled = true;
            }
            else
            {
                output_information.Text = "请选择要移动的文件~";
            }
        }
        private void copy_Click(object sender, EventArgs e)
        {
            List<string> copy_file_lst = new List<string>();
            List<string> change_name = new List<string>();
            string selected_txt = null;
            output_name.Text = "操作状态";
            int i = 0;
            for (i = 0; i < filelist.Items.Count; i++)
                if (filelist.GetItemChecked(i))
                {
                    opera.set_selected_items(copy_file_lst, filelist.GetItemText(filelist.Items[i]));
                }
            output_information.Text = "等待服务器反应中";
            if (copy_file_lst.Count > 0)
            {
                move_win mov_win = new move_win();
                this.Enabled = false;
                if (mov_win.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        opera.batch_copy(mybucket, copy_file_lst, change_name, move_win.name_prefix, move_win.source_name_rule, move_win.name_num_rule, move_win.name_postfix);
                    }
                    catch (Exception ex)
                    {
                        opera.except_process(ex);
                        this.Close();
                    }
                    i = 0;
                    foreach (string name in change_name)
                    {
                        if (filelist.Items.Contains(name))
                        {
                            selected_txt += copy_file_lst[i];
                        }
                        else
                        {
                            filelist.Items.Add(name);
                        }
                        i++;
                    }
                    if (selected_txt == null)
                    {
                        output_information.Text = "成功复制文件" + Environment.NewLine;
                        i = 0;
                        foreach (string name in change_name)
                        {
                            output_information.Text += (copy_file_lst[i] + "------------>" + change_name[i] + Environment.NewLine);
                        }
                    }
                    else
                    {
                        output_information.Text = "这些文件没有成功复制：      ";
                        output_information.Text += selected_txt;
                    }

                }
                else
                {
                    output_information.Text = "你取消了复制操作";
                }

            }
            else
            {
                output_information.Text = "请选择要复制的文件~";
            }
            this.Enabled = true;

        }

        private void upload_Click(object sender, EventArgs e)
        {
            string selected_txt = "";
            List<string> change_name = new List<string>();
            List<string> dst = new List<string>();
            int i = 0;
            output_name.Text = "操作状态";
            qiniuupload.InitialDirectory = Environment.CurrentDirectory + "\\";
            qiniuupload.Filter = "jpg图片|*.jpg|png图片|*.png|所有文件|*.*";
            qiniuupload.RestoreDirectory = true;
            if (qiniuupload.ShowDialog() == DialogResult.OK)
            {
                Encoding dstEncoding = Encoding.UTF8;
                Encoding defaultEncoding = Encoding.Default;
                opera.OutputByEncoding(defaultEncoding, dstEncoding, qiniuupload.FileNames, dst);
                move_win mov_win = new move_win();
                this.Enabled = false;
                if (mov_win.ShowDialog() == DialogResult.OK)
                {
                    output_information.Text = "等待服务器反应中";
                    try
                    {
                        opera.ResumablePutFile(mybucket, dst, change_name, move_win.name_prefix, move_win.source_name_rule, move_win.name_num_rule, move_win.name_postfix);
                    }
                    catch (Exception ex)
                    {
                        opera.except_process(ex);
                        this.Close();
                    }
                    foreach (string name in change_name)
                    {
                        if (filelist.Items.Contains(name))
                        {
                            selected_txt += qiniuupload.FileNames[i];
                        }
                        else
                        {
                            filelist.Items.Add(name);
                        }
                        i++;
                    }
                    if (selected_txt == "")
                    {
                        output_information.Text = "成功上传文件";
                    }
                    else
                    {
                        output_information.Text = "这些文件没有成功上传：      ";
                        output_information.Text += selected_txt;
                    }
                }
                else
                {
                    output_information.Text = "你取消了上传操作.";
                }
                this.Enabled = true;
            }
            else
            {
                output_information.Text = "你取消了上传操作.";
            }
        }
        public delegate void downloadDelegate(List<string> downloadlist, string path);
        private void download_Click(object sender, EventArgs e)
        {
            List<string> down_file_lst = new List<string>();
            string path;
            output_name.Text = "操作状态";
            int i = 0;
            for (i = 0; i < filelist.Items.Count; i++)
                if (filelist.GetItemChecked(i))
                {
                    opera.set_selected_items(down_file_lst, filelist.GetItemText(filelist.Items[i]));
                }
            output_information.Text = "正在下载";
            path = ConfigHelper.GetConnectionStringsConfig("downpath");
            if (path == "")
            {
                path = Environment.CurrentDirectory + "\\" + "download\\";
                Directory.CreateDirectory(path);
            }
            if (System.IO.Directory.Exists(path))
            {
                downloadDelegate downloadinvo = new downloadDelegate(downloadUI.myshow);
                BeginInvoke(downloadinvo, down_file_lst, path);
            }
            else
            {
                output_information.Text = path + "不存在.";
            }
        }
        private void filelist_rightclick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)//判断是否右键点击
            {
                Point p = e.Location;//获取点击的位置
                int index = filelist.IndexFromPoint(p);//根据位置获取右键点击项的索引
                filelist.SelectedIndex = index;//设置该索引值对应的项为选定状态
                this.right_click_index = index;
                //checkedListBox1.SetItemChecked(index, true);//如果需要的话这句可以同时设置check状态
            }
            if (e.Button == MouseButtons.Left)//判断是否右键点击
            {
                dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
                count = 0;
                left_click = 1;
                mouse_mul_select = 0;
                dispatcherTimer.Start();
            }
        }

        private void name_cp_Click(object sender, EventArgs e)
        {
            string right_click_name = null;
            right_click_name = filelist.Items[this.right_click_index].ToString();
            Clipboard.SetDataObject(ConfigHelper.GetConnectionStringsConfig("server_url") + right_click_name);
            MessageBox.Show("文件地址复制成功");
        }

        private void UIoption_Click(object sender, EventArgs e)
        {
            option myoption = new option();
            if (myoption.ShowDialog() == DialogResult.OK)
            {
            }
        }

        private void bucket_move_Click(object sender, EventArgs e)
        {
            List<string> move_file_lst = new List<string>();
            List<string> change_name = new List<string>();
            List<string> lst = new List<string>();
            List<int> change_fail_index = new List<int>();
            bool opera_enable = true;
            string selected_txt = null;
            int i = 0;
            output_name.Text = "操作状态";
            try
            {
                opera.list(ConfigHelper.GetConnectionStringsConfig("dst_bucket"), lst);
            }
            catch
            {
                output_information.Text = "abort!!输入正确的移动目的bucket";
                opera_enable = false;
            }
            if (opera_enable)
            {
                for (i = 0; i < filelist.Items.Count; i++)
                    if (filelist.GetItemChecked(i))
                    {
                        opera.set_selected_items(move_file_lst, filelist.GetItemText(filelist.Items[i]));
                    }
                output_information.Text = "等待服务器反应中";
                if (move_file_lst.Count > 0)
                {
                    move_win mov_win = new move_win();
                    this.Enabled = false;
                    if (mov_win.ShowDialog() == DialogResult.OK)
                    {
                        try
                        {
                            opera.move(mybucket, move_file_lst, ConfigHelper.GetConnectionStringsConfig("dst_bucket"), change_name, move_win.name_prefix, move_win.source_name_rule, move_win.name_num_rule, move_win.name_postfix);
                        }
                        catch (Exception ex)
                        {
                            opera.except_process(ex);
                            this.Close();
                        }
                        i = 0;
                        foreach (string name in change_name)
                        {
                            if (lst.Contains(name))
                            {
                                selected_txt += move_file_lst[i];
                                change_fail_index.Add(filelist.Items.IndexOf(move_file_lst[i]));
                            }
                            i++;
                        }
                        int j;
                        if (change_fail_index.Count > 0)
                        {
                            for (i = 0, j = 0; i < move_file_lst.Count; i++)
                            {
                                if (i == change_fail_index[j])
                                {
                                    j++;
                                }
                                else
                                {
                                    filelist.Items.Remove(move_file_lst[i]);
                                }
                            }
                        }
                        else
                        {
                            for (i = 0; i < move_file_lst.Count; i++)
                            {
                                filelist.Items.Remove(move_file_lst[i]);
                            }
                        }
                        if (selected_txt == null)
                        {
                            output_information.Text = "成功移动文件" + Environment.NewLine;
                            i = 0;
                            foreach (string name in change_name)
                            {
                                output_information.Text += (mybucket + ":" + move_file_lst[i] + "------------>" + ConfigHelper.GetConnectionStringsConfig("dst_bucket") + " :" + change_name[i] + Environment.NewLine);
                                i++;
                            }
                        }
                        else
                        {
                            output_information.Text = "这些文件没有成功移动：      ";
                            output_information.Text += selected_txt;
                        }

                    }
                    else
                    {
                        output_information.Text = "你取消了bucket间的移动操作";
                    }

                }
                else
                {
                    output_information.Text = "请选择要移动的文件~";
                }
                this.Enabled = true;
            }
        }

        private void bucket_copy_Click(object sender, EventArgs e)
        {
            List<string> copy_file_lst = new List<string>();
            List<string> change_name = new List<string>();
            List<string> lst = new List<string>();
            bool opera_enable = true;
            string selected_txt = null;
            int i = 0;
            output_name.Text = "操作状态";
            try
            {
                opera.list(ConfigHelper.GetConnectionStringsConfig("dst_bucket"), lst);
            }
            catch
            {
                output_information.Text = "abort!!输入正确的复制目的bucket";
                opera_enable = false;
            }
            if (opera_enable)
            {
                for (i = 0; i < filelist.Items.Count; i++)
                    if (filelist.GetItemChecked(i))
                    {
                        opera.set_selected_items(copy_file_lst, filelist.GetItemText(filelist.Items[i]));
                    }
                output_information.Text = "等待服务器反应中";
                if (copy_file_lst.Count > 0)
                {
                    move_win mov_win = new move_win();
                    this.Enabled = false;
                    if (mov_win.ShowDialog() == DialogResult.OK)
                    {
                        try
                        {
                            opera.copy(mybucket, copy_file_lst, ConfigHelper.GetConnectionStringsConfig("dst_bucket"), change_name, move_win.name_prefix, move_win.source_name_rule, move_win.name_num_rule, move_win.name_postfix);
                        }
                        catch (Exception ex)
                        {
                            opera.except_process(ex);
                            this.Close();
                        }
                        i = 0;
                        foreach (string name in change_name)
                        {
                            if (lst.Contains(name))
                            {
                                selected_txt += copy_file_lst[i];
                            }
                            i++;
                        }
                        if (selected_txt == null)
                        {
                            output_information.Text = "成功复制文件" + Environment.NewLine;
                            i = 0;
                            foreach (string name in change_name)
                            {
                                output_information.Text += (mybucket + ":" + copy_file_lst[i] + "------------>" + ConfigHelper.GetConnectionStringsConfig("dst_bucket") + " :" + change_name[i] + Environment.NewLine);
                                i++;
                            }
                        }
                        else
                        {
                            output_information.Text = "这些文件没有成功复制：      ";
                            output_information.Text += selected_txt;
                        }

                    }
                    else
                    {
                        output_information.Text = "你取消了bucket间的复制操作";
                    }

                }
                else
                {
                    output_information.Text = "请选择要复制的文件~";
                }
                this.Enabled = true;
            }
        }

        private void export_name_Click(object sender, EventArgs e)
        {
            List<string> export_file_lst = new List<string>();
            move_win mov_win = new move_win(false, true);
            this.Enabled = false;
            for (int i = 0; i < filelist.Items.Count; i++)
                if (filelist.GetItemChecked(i))
                {
                    opera.set_selected_items(export_file_lst, filelist.GetItemText(filelist.Items[i]));
                }
            output_information.Text = "等待服务器反应中";
            if (export_file_lst.Count > 0)
            {
                if (mov_win.ShowDialog() == DialogResult.OK)
                {
                    output_information.Text = "";
                    for (int i = 0; i < export_file_lst.Count; i++)
                        output_information.Text += ConfigHelper.GetConnectionStringsConfig("server_url") + export_file_lst[i] + move_win.name_postfix + Environment.NewLine;
                }
                else
                {
                    output_information.Text = "你取消了导出操作";
                }
                this.Enabled = true;
            }
            else
            {
                output_information.Text = "请选择文件。";
            }
        }
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            count++;
            if (count == 2 && left_click == 1)
            {
                mouse_mul_select = 1;
                output_information.Text = "进入连续选择状态.";
            }
        }
        private void mouse_up(object sender, MouseEventArgs e)
        {
            left_click = 0;
            Point p = e.Location;//获取点击的位置
            int index = filelist.IndexFromPoint(p);//根据位置获取右键点击项的索引
            dispatcherTimer.Stop();
            output_information.Text = "";
            if (mouse_mul_select == 1)
            {
                mouse_mul_select = 0;
                if (index > 0)
                    filelist.SetItemChecked(index, true);//如果需要的话这句可以同时设置check状态
            }
        }
        private void mouse_move(object sender, MouseEventArgs e)
        {
            if (mouse_mul_select == 1)
            {
                Point p = e.Location;//获取点击的位置
                int index = filelist.IndexFromPoint(p);//根据位置获取右键点击项的索引
                if (index > 0)
                {
                    if (!filelist.GetItemChecked(index))
                    {
                        filelist.SetItemChecked(index, true);//如果需要的话这句可以同时设置check状态
                    }
                }
            }
        }
    }
}
